<?php
/**
 * @package Spider Contacts
 * @author Web-Dorado
 * @copyright (C) 2012 Web-Dorado. All rights reserved.
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 **/
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.model' );





class spidercontactsModelshowContact extends JModel

{

	function showContact()

	{

		

		

	
 $mainframe = JFactory::getApplication();
	

	

	$option=JRequest::getVar('option');

	
	$params = new jsshparams;
	



	
$contact=JRequest::getVar('contact', 0);




//	$rev_page=JRequest::getVar('rev_page', 1);



	if ($contact)
	{
$contact_id = $contact;
	}
	else
	{

	$contact_id= JRequest::getVar('contact_id', 0);
	}
	
	$db = JFactory::getDBO();



	$query = "SELECT #__spidercontacts_contacts.*, #__spidercontacts_contacts_categories.name as cat_name FROM #__spidercontacts_contacts left join #__spidercontacts_contacts_categories on  #__spidercontacts_contacts.category_id=#__spidercontacts_contacts_categories.id where

	#__spidercontacts_contacts.id='$contact_id' and #__spidercontacts_contacts.published = '1' ";



	$db->setQuery( $query );



	$rows = $db->loadObjectList();



	if ($db->getErrorNum())

		{

			echo $db->stderr();

			return false;

		}

		foreach($rows as $row)

		{

			$category_id=$row->category_id;

		}



		$query= "SELECT * FROM #__spidercontacts_contacts_categories WHERE id = '".$db->getEscaped($category_id)."' ";

		

		$db->setQuery($query);



		$row1 = $db->loadAssoc();

		$session = JFactory::getSession();	

		

		$category_name=$row1['name'];

		$message_title= JRequest::getVar('mes_title','');

		$email1 = JRequest::getVar('email', '');

		if ($email1 =='No Email')

		$email = '';

		else $email = $email1;

		//echo $rows[0]->want_email;

		$phone1 = JRequest::getVar('phone', '');

		if ($phone1 == 'No Phone')

		$phone =  '';

		else $phone = $phone1;

		$full_name1=JRequest::getVar('full_name','');

		if ($full_name1 == "No Name")

		$full_name = ' ';

		else $full_name = $full_name1;

		$is_message=JRequest::getVar('is_message',false);

		$cont_pref = JRequest::getVar('cont_pref','');

		//echo $cont_pref.'adadasd';

		if ($cont_pref == '1')

		$cont_pref_db = 'Phone';

		else if ($cont_pref == '0')

		$cont_pref_db = 'Email';

		else if ($cont_pref == '1')

		$cont_pref_db = 'Either';

		else $cont_pref_db= ' ';

		//echo $cont_pref_db.'123123123123123312';

		$message_text=JRequest::getVar('message_text','');

		$row_mes = JTable::getInstance('messages', 'Table');

		$row_mes->sender=$full_name;

		$row_mes->text=$message_text;

		$row_mes->to_contact=$contact_id;

		$row_mes->title=$message_title;

		$row_mes->email = $email;

		$row_mes->phone = $phone;

		$row_mes->cont_pref = $cont_pref_db;

		

		$code=JRequest::getVar('code','');

	





				if($code!='' and $full_name!='' and $code==$session->get( 'captcha_code', '' ) )

					{

						if (!$row_mes->store())

							{

								echo "<script> alert('".$row_mes->getError()."')</script>\n";

								exit();

							}

						else

							{

								if ($rows[0]->want_email!=0 && $rows[0]->email!='')

								{

									if ($email!='')

									{

										

									jimport('joomla.utilities.utility');

										$config = JFactory::getConfig();

											$from = $email;

											$fromname = $full_name;

											$recipient = $rows[0]->email;
											
											$subject = $message_title;

											$replyto = $email;

											$replytoname = $full_name;

											$body   = '<span>A Message from <u style="font-size:150%;" >'.$fromname.'</u>  </span>



<table >
<tr>
<td  class="paramlist_key"><span class="editlinktip">&nbsp;</span></td>
<td class="paramlist_value"><b>Sender Details</b></td>
</tr>


<tr>



<td   style=" background-color:  #BEC8D1; text-align: right; width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right" >



Sender Phone:



</td>



<td style=" padding: 3px;">


'.$phone.'



</td>



</tr>


<tr >



<td   style=" background-color:  #BEC8D1; text-align: right;  width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right" >



Sender Email:



</td>



<td style=" padding: 3px;" >


'.$email.'



</td>



</tr>


<tr>



<td  style=" background-color:  #BEC8D1; text-align: right;  width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right" >



Sender Contact Preference:



</td>



<td style=" padding: 3px;" >



'.$cont_pref_db.'


</td>



</tr>

<tr>
<td  style=" background-color:  #BEC8D1; text-align: right;  width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="40%" class="paramlist_key"><span class="editlinktip">&nbsp;</span></td>
<td style=" padding: 3px;" class="paramlist_value"><b>Message Details</b></td>
</tr>


<tr>



<td  style=" background-color:  #BEC8D1; text-align: right;  width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right" >



To Contact:



</td>



<td style=" padding: 3px;" >


'.$recipient.'



</td>



</tr>

<tr>



<td   style=" background-color:  #BEC8D1; text-align: right; width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right" >



Category:



</td>



<td style=" padding: 3px;" >


'.$category_name.'



</td>



</tr>

<tr>



<td   style=" background-color:  #BEC8D1; text-align: right; width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right"  >



Title:



</td>



<td style=" padding: 3px;" >


'.$subject.'



</td>



</tr>

<tr>



<td   valign="top" style=" background-color:  #BEC8D1; text-align: right;  width: 30%; color: #666; font-weight: bold; border-bottom: 1px solid #e9e9e9;  border-right: 1px solid #e9e9e9; padding: 3px;" width="100" align="right"  >



Message:



</td>



<td style=" padding: 3px;" >


'.$message_text.'



</td>



</tr>

</table>
';

											

											

												if ( JUtility::sendMail($from, $fromname, $recipient, $subject, $body, 1,$cc=null, $bcc=null, $attachment=null, $replyto, $replytoname) )
												 {

													

													
													$mainframe->redirect($_SERVER["REQUEST_URI"], $msg=''.JText::_('SH_MES_EMAILED').'');

												}

												 else {
													 $mainframe->redirect($_SERVER["REQUEST_URI"], $msg=''.JText::_('SH_MES_SEND_NOT_EMAILED').'');

												}

										

										

										}

									else $mainframe->redirect($_SERVER["REQUEST_URI"], $msg=''.JText::_('SH_MES_SEND').'');

									

									}

									
								$mainframe->redirect($_SERVER["REQUEST_URI"], $msg=''.JText::_('SH_MES_SEND').'');

							}

					}









	

				

		return array($rows,$option, $params,$category_name);



	}

}

?>

